home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 3 / QRZ Ham Radio Callsign Database - Volume 3.iso / world / amiga / 7pl204am.lzh / 7PLUS204 / SOURCE / 7PLUS.C next >
C/C++ Source or Header  |  1992-11-26  |  15KB  |  476 lines

  1. /* Version */
  2. #define _7P_VMAJOR 2
  3. #define _7P_VMINOR 04
  4. #define VERSION "2.04"
  5. #define DATE "921121"
  6.  
  7. /*********************************************************************
  8. *** 7PLUS ASCII- Encoder/Decoder, Axel Bauda, DG1BBQ @DB0CL.DEU.EU  ***
  9. **********************************************************************
  10. ***
  11. *** Compile:
  12. *** --------
  13. ***
  14. *** -------
  15. *** | IBM |  : SMALL memory model
  16. *** -------
  17. ***            Turbo C++ and Turbo C 2.0:
  18. ***            MAKE -FTC_MAKE
  19. ***
  20. ***      also: Turbo C++:
  21. ***            Compile in IDE using 7PLUS.PRJ
  22. ***
  23. ***      or  : Turbo C 2.0:
  24. ***            Rename TC2.PRJ to 7PLUS.PRJ and compile in IDE
  25. ***
  26. *** ---------
  27. *** | ATARI |  Pure C 1.0/Turbo C 2.0 : Use 7PLST.PRJ
  28. *** ---------
  29. *** --------
  30. *** | UNIX |   support by Torsten H. Bischoff, DF6NL @ DB0BOX
  31. *** --------
  32. *** Supported systems are:
  33. ***   Interactive UNIX 386
  34. ***   SCO XENIX 386
  35. ***   VAX BSD 4.3/ Ultrix 4.1
  36. ***
  37. *** Compile: make -fu_make
  38. ***
  39. *** ---------
  40. *** | AMIGA |  Lattice SAS/C V5.1b
  41. *** ---------
  42. ***            Compile : LC -v -d_AMIGA_ $.o
  43. ***                      BLINK c.o LIB lib:lc.lib,lib:amiga2.0.lib ND SC SD
  44. ***
  45. ***
  46. *** Other system: Find out for yourself :->  Good luck!
  47. ***
  48. *** All systems:
  49. *** Signed or unsigned char per default is: don't care.
  50. *** No floating point lib required.
  51. ***
  52. *** TABSIZE when editing: 2; don't insert real TABs (^I), use spaces instead.
  53. ***
  54. *** When porting or modifying this source, make SURE it can still be compiled
  55. *** on all systems! Do this by using #ifdef directives! Please let me know
  56. *** about the modifications or portations, so I can include them in the origi-
  57. *** nal 7PLUS.
  58. ***
  59. **********************************************************************
  60. ***  7PLUS ASCII-Encoder/Decoder, Axel Bauda, DG1BBQ @DB0CL.DEU.EU  ***
  61. **********************************************************************
  62. ***
  63. *** File converter for transfer of arbitrary binary data
  64. *** via store & forward.
  65. ***
  66. *** 7PLUS is HAMWARE. No commercial use. Pass on only in it's entirety!
  67. *** There is no warranty for the proper functioning. Use at own risk.
  68. ***
  69. *** Tnx to:
  70. *** DL1XAO, DB7YAH, DG3YFU and DL6YAV for testing and tips regarding
  71. *** ATARI ST,
  72. *** DB5ZP, DD3IF for their efforts concerning the AMIGA implementation.
  73. *** DF6NL for adding UNIX-compatibility,
  74. *** and more not mentioned here.
  75. ***
  76. ***
  77.  */
  78.  
  79. #include "7plus.h"
  80.  
  81. /** globals **/
  82. uint    crctab[256];
  83. byte    decode[256];
  84. byte    code  [216];
  85. byte    extended = INDICATE;
  86. size_t  buflen;
  87. char    _drive[MAXDRIVE], _dir[MAXDIR], _file[MAXFILE], _ext[MAXEXT];
  88. char    spaces[] = "                                                   ";
  89. char    *endstr;
  90. char    delimit[] = "\n";
  91. char    cant[] = "\007\n'%s': Can't open. Break.\n";
  92. char    notsame[] = "\007Filesize in %s differs from the original file!\nBreak.\n";
  93. int     noquery = 0;
  94. int     force   = 0;
  95. int     fls     = 0;
  96. int     autokill= 0;
  97. struct  m_index *index;
  98.  
  99. #ifdef __TOS__
  100.   int   nowait  = 0;
  101. #endif
  102.  
  103. char logon_ctrl[] =
  104. #ifdef __MSDOS__
  105. "\n╔════════════════════════════════════════════════╗\n║%s║\n║%s║\n╚══════════\
  106. ══════════════════════════════════════╝\n";
  107. #else
  108.  #ifdef __TOS__
  109. "\033p\033v\n%s\n%s\n\033q\n";
  110.  #else
  111.   #ifdef _AMIGA_
  112. "\n\033[3m%s\n%s\033[0m\n\n";
  113.   #else    /* neither __MSDOS__ or __TOS__ or _AMIGA_ */
  114. "\n[]----------------------------------------------[]\n|%s|\n|%s|\n[]---------\
  115. -------------------------------------[]\n";
  116.   #endif   /* ifdef _AMIGA_   */
  117.  #endif   /* ifdef __TOS__   */
  118. #endif   /* ifdef __MSDOS__ */
  119.  
  120. char *logon[] = { "   7PLUS - file converter for store & forward   ",
  121.                   " version "VERSION" ("DATE"), (C) DG1BBQ@DB0CL.DEU.EU " };
  122.  
  123. char *help[] = {
  124. #ifdef _AMIGA_
  125. "\n\033[1mExamples:\n",
  126. "7plus <FILE.7pl>      Decode <FILE.7pl>. create <FILE.err>, if errors detected\n",
  127. "7plus <FILE.p01>      Recombine & decode.                    \"\n",
  128. "7plus <FILE>          Automatically correct and decode.\n",
  129. "                      If a file named <FILE> exists, encode it.\n",
  130. "7plus <FILE>          Encode (automatically split into 10K chunks).\n",
  131. "             -s 30    30 lines/file (max 512 lines/file).\n",
  132. "             -sp 3    3 parts of roughly equal size (max 255 parts).\n",
  133. "             -sb 3000 Parts of roughly 3000 bytes (max 36000).\n",
  134. "7plus <FILE.err> dh0:/pr/ Create correction file. look for <FILE> in dh0:/pr/.\n",
  135. "7plus <FILE.cor>      Use <FILE.cor> to correct metafile <FILE.7mf>.\n",
  136. "7plus <FILE.7pl> -c   Use 7PLUS-file as a correction file.\n",
  137. "7plus <LOGFILE> -x <TEXT> Extract 7plus-files from <LOGFILE>. Only extract a\n",
  138. "                      file, if its name contains <TEXT>. Omit <TEXT> to\n",
  139. "                      extract files in <LOGFILE>.\n",
  140. "7plus <FILE.err> <FILE2.err> -j  Add contents of error report <FILE2err> to\n",
  141. "                      error report <FILE.err>.\n",
  142. "7plus <FILE.err> -j   Add contents of error reports <FILE.eXX> to error\n",
  143. "                      report <FILE.err> (multiple join).\n",
  144. #ifdef _CHSIZE_OK
  145. "7plus <FILE.7mf>      Create new error report from metafile.\n\n",
  146. #else
  147. "7plus <FILE.7ix>      Create new error report from indexfile.\n\n",
  148. #endif
  149. "Other Options:\n",
  150. "-k           Automatically kill all obsolete files.\n",
  151. "-p           Use Packet line separator CR for encoded files. Should be\n",
  152. "             used, when uploading files to the BBS in binary mode!\n",
  153. "-r 5         When encoding, only create part 5. Be sure to split the same\n",
  154. "             way as for the first upload!\n",
  155. "-t /ex       Append string '/ex' to encoded files (BBS file termination).\n",
  156. "-tb <FILE>   Get head and footlines from <FILE> when encoding. See manual.\n",
  157. "-y           Assume YES on all queries.\n",
  158. "-#           Create 7PLUS.FLS when encoding. See Manual.\n",
  159. "\033[0m\n\n",
  160. #else
  161. "\nExamples:\n",
  162. "7plus file.7pl          Decode file.7pl. create file.err, if errors detected.\n",
  163. "7plus file.p01          Recombine & decode.              \"\n",
  164. "7plus file              Automatically correct and decode.\n",
  165. "                        If a file named 'file' exists, encode it.\n",
  166. "7plus file.txt          Encode (automatically split into 10K chunks).\n",
  167. "               -s 30    30 lines/file (max 512 lines/file).\n",
  168. "               -sp 3    3 parts of roughly equal size (max 255 parts).\n",
  169. "               -sb 3000 Parts of roughly 3000 bytes (max 36000).\n",
  170. "7plus file.err c:\\pr\\   Create correction file. look for file.txt in c:\\pr\\.\n",
  171. "7plus file.cor          Use file.cor to correct metafile file.7mf.\n",
  172. "7plus file.7pl -c       Use 7PLUS-file as a correction file.\n",
  173. "7plus logfile -x text   Extract 7plus-files from logfile. Only extract a file\n",
  174. "                        if its name contains 'text'. Omit 'text' to extract\n",
  175. "                        all files in logfile.\n",
  176. "7plus file.err file2.err -j  Add contents of error report file2.err to\n",
  177. "                        error report file.err.\n",
  178. "7plus file.err -j       Add contents of error reports file.eXX to error\n",
  179. "                        report file.err (multiple join).\n",
  180. #ifdef _CHSIZE_OK
  181. "7plus file.7mf          Create new error report from metafile.\n\n",
  182. #else
  183. "7plus file.7ix          Create new error report from indexfile.\n\n",
  184. #endif
  185. "Other Options:\n",
  186. "-k           Automatically kill all obsolete files.\n",
  187. #ifdef __TOS__
  188. "-n           Don't wait for a keystroke at termination.\n",
  189. #endif
  190. "-p           Use Packet line separator CR for encoded files. Should be\n",
  191. "             used, when uploading files to the BBS in binary mode!\n",
  192. "-r 5         When encoding, only create part 5. Be sure to split the same\n",
  193. "             way as for the first upload!\n",
  194. "-t /ex       Append string '/ex' to encoded files (BBS file termination).\n",
  195. "-tb file     Get head and footlines from 'file' when encoding. See manual.\n",
  196. "-y           Assume YES on all queries.\n",
  197. "-#           Create 7PLUS.FLS when encoding. See manual.\n",
  198. #endif
  199. NULLCP
  200. };
  201.  
  202.  
  203. /* Depending on the system, it may be nessesary to prompt the user for a
  204.    keystroke, before terminating, because user wouldn't be able to read
  205.    the outputs to the screen, when the window closes at termination.
  206.    However, the '-n' option overrides this. */
  207. int main (int argc, char **argv)
  208. {
  209. #ifdef __TOS__
  210.   int ret;
  211.   
  212.   ret = go_at_it (argc, argv);
  213.   if ((!nowait || noquery) && !system(NULLCP))
  214.   {
  215.     printf("\n\033p Hit any key \033q");
  216.     getch();
  217.   }
  218.   return (ret);
  219. #else
  220.   return (go_at_it (argc, argv));
  221. #endif
  222. }
  223.  
  224. /* This is the real main() */
  225. int go_at_it (int argc, char **argv)
  226. {
  227.   char *p, *q, *r, *s, *t;
  228.   int  i, extract, join, part, cor;
  229.   long blocksize;
  230.  
  231.   i = extract = join = part = cor = 0;
  232.   p = q = r = s = t = endstr = NULLCP;
  233.  
  234.  
  235.   /* Default blocksize (abt 10000 bytes) */
  236.   blocksize = 138 * 62;
  237.  
  238.   printf (logon_ctrl, logon[0], logon[1]);
  239.  
  240.   while (++i<argc)
  241.   {
  242.     if (*argv[i] != '-')
  243.     {
  244.       if (!p)
  245.       {
  246.         p = argv[i];  /* Name of file to de/encode */
  247.         continue;
  248.       }
  249.       if (!r)
  250.       {
  251.         r = argv[i];  /* Searchpath for non-coded file. Needed for */
  252.         continue;     /* generating correction file */
  253.       }
  254.     }
  255.  
  256.     if (!stricmp (argv[i], "-S")) /* Split option */
  257.     {
  258.       i++;
  259.       if (i == argc)
  260.       {
  261.         blocksize = 512 * 62;  /* No parameter, set max blocksize */
  262.         i--;
  263.       }
  264.       else
  265.         blocksize = atol (argv[i]) * 62; /* Set blocksize to parameter */
  266.     }
  267.  
  268.     if (!stricmp (argv[i], "-SP")) /* Split in equal parts */
  269.     {
  270.       i++;
  271.       if (i == argc)
  272.       {
  273.         blocksize = 0; /* No parameter, no user defined split */
  274.         i--;
  275.       }
  276.       else
  277.         blocksize = 50000L + atol (argv[i]); /* Number of parts to encode */
  278.     }
  279.  
  280.     if (!stricmp (argv[i], "-SB")) /* Split in parts of n bytes */
  281.     {
  282.       i++;
  283.       if (i == argc)
  284.         i--;
  285.       else
  286.         blocksize = (atol (argv[i]) /71 -2) *62;
  287.     }
  288.  
  289.     if (!stricmp (argv[i], "-R")) /* Only reencode part n */
  290.     {
  291.       i++;
  292.       if (i == argc)
  293.         i--;
  294.       else
  295.         part = atoi (argv[i]);
  296.     }
  297.  
  298.     if (!stricmp (argv[i], "-TB")) /* File to get head and foot lines from */
  299.     {
  300.       i++;
  301.       if (i == argc)
  302.         i--;
  303.       else
  304.         t = argv[i];
  305.     }
  306.  
  307.     if (!stricmp (argv[i], "-T")) /* Define BBSs termination string */
  308.     {
  309.       i++;
  310.       if (i == argc)
  311.         i--;
  312.       else
  313.       {
  314.         endstr = malloc ((int) strlen (argv[i]) +1);
  315.         strcpy (endstr, argv[i]);
  316.       }
  317.     }
  318.  
  319.     if (!stricmp (argv[i], "-#")) /* Create 7PLUS.FLS. Contents e.g.:     */
  320.       fls = 1;                    /* 10 TEST */
  321.                                   /* for TEST.EXE encoded into 10 parts   */
  322.  
  323.     if (!stricmp (argv[i], "-C")) /* Use 7PLUS-file as a correction file  */
  324.       cor = 1;
  325.  
  326.     if (!stricmp (argv[i], "-K")) /* Kill obsolete files */
  327.       autokill = 1;
  328.  
  329.     if (!stricmp (argv[i], "-F")) /* Force usage of correction file */
  330.       force = 1;
  331.  
  332.     if (!stricmp (argv[i], "-J")) /* Join two error reports */
  333.       join = 1;
  334.  
  335.     #ifdef __TOS__
  336.     if (!stricmp (argv[i], "-N")) /* Don't wait for a key at termination  */
  337.       nowait = 1;
  338.     #endif
  339.  
  340.     if (!stricmp (argv[i], "-P")) /* Write encoded files in Packet format */
  341.       sprintf (delimit, "\r");    /* for direct binary upload. */
  342.  
  343.     if (!stricmp (argv[i], "-X")) /* Extract 7plus-files from log-file    */
  344.       extract = 1;
  345.  
  346.     if (!stricmp (argv[i], "-VMAJOR")) /* Return version number +100      */
  347.       return (_7P_VMAJOR +100);
  348.  
  349.     if (!stricmp (argv[i], "-VMINOR")) /* Return subversion number + 100  */
  350.       return (_7P_VMINOR+100);
  351.  
  352.     if (!stricmp (argv[i], "-Y")) /* Always assume YES on queries.*/
  353.       noquery = 1;
  354.  
  355.   }
  356.   if (!p ) /* No File specified, show help */
  357.   {
  358.     int n = 4;
  359.     i = 0;
  360.     while (help[i])
  361.     {
  362.       if (++n == 24 && !noquery)
  363.       {
  364.         printf ("Press RETURN key to continue....\r");
  365.         fflush (stdout);
  366.         while (!getch ());
  367.         fflush (stdin);
  368.         n = 0;
  369.         printf ("                             \r");
  370.       }
  371.       printf (help[i++]);
  372.     }
  373.     return (0);
  374.   }
  375.  
  376.   if ((s = malloc (50000U)) == NULLCP)
  377.   {
  378.      printf ("\007Argh error: Not enough memory present! Can't continue.....\n");
  379.      exit (12);
  380.   }
  381.   free (s);
  382.  
  383.   if ((index = (struct m_index *)malloc (sizeof (struct m_index))) == NULL)
  384.   {
  385.     printf ("\007Argh error: Not enough memory present! Can't continue.....\n");
  386.     exit (12);
  387.   }
  388.  
  389.   buflen = 16384;
  390.  
  391.   init_crctab (); /* Initialize table for CRC-calculation */
  392.   init_decodetab (); /* decoding-table */
  393.   init_codetab   (); /* encoding-table */
  394.  
  395.   if (extract)
  396.     if (p)
  397.       return (extract_files (p, r));
  398.     else
  399.     {
  400.       printf ("\007File to extract from not specified. Break.\n");
  401.       return (6);
  402.     }
  403.   if (join)
  404.     return (join_control (p, r));
  405.  
  406.   /* Does the filename contain an extension? */
  407.   if ((q = strrchr (p, '.')) != NULLCP)
  408.   {
  409.     if (cor)
  410.       return (correct_meta (p, 0));
  411.  
  412.     if (!strnicmp (".cor", q, 4) ||
  413.         (toupper(*(q+1)) == 'C' && isxdigit(*(q+2)) && isxdigit(*(q+3))))
  414.       return (correct_meta (p, 1));
  415.  
  416.     /* Call decode_file() if ext ist 7PL, P01, else encode_file() */
  417.     if (!strnicmp (".7pl", q, 4) || !strnicmp (".p01", q, 4))
  418.       return (control_decode (p));
  419.  
  420.     #ifdef _CHSIZE_OK
  421.      if (!strnicmp (".7mf", q, 4))
  422.        return(make_new_err (p));
  423.     #else
  424.      if (!strnicmp (".7ix", q, 4))
  425.        return(make_new_err (p));
  426.     #endif
  427.  
  428.     if (!strnicmp (".x", q, 3))
  429.       return (extract_files (p, r));
  430.  
  431.     return (encode_file (p, blocksize, r, part, t));
  432.   }
  433.   else
  434.   {
  435.     if (!test_exist (p)) /* no EXT, but file exists on disk, then encode */
  436.       return (encode_file (p, blocksize, r, part, t));
  437.     else
  438.       return (control_decode (p));
  439.   }
  440. }
  441.  
  442. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
  443.  *+ Possible return codes:                                                 +*
  444.  *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
  445.  
  446.   0 No errors detected.
  447.   1 Write error.
  448.   2 File not found.
  449.   3 7PLUS header not found.
  450.   4 File does not contain expected part.
  451.   5 7PLUS header corrrupted.
  452.   6 No filename for extracting defined.
  453.   7 invalid error report / correction / index file.
  454.   8 Max number of parts exceeded.
  455.   9 Bit 8 stripped.
  456.  10 User break in test_file();
  457.  11 Error report generated.
  458.  12 Only one or no error report to join
  459.  13 Error report/cor-file does not relate to the same original file
  460.  14 Couldn't write 7plus.fls
  461.  15 Filesize of original file and the size reported in err/cor-file not equal
  462.  16 Correction not successful.
  463.  17 No CRC found in err/cor-file.
  464.  18 Timestamp in metafile differs from that in the correction file.
  465.  19 Metafile already exists.
  466.  
  467.  1XX If invoked with '-VMAJOR' option, 7PLUS returns version number.
  468.      e.g.: 101  <--- v1.5
  469.          : 108  <--- v8.8
  470.  
  471.      Invoked with '-VMINOR':
  472.      e.g.: 150  <--- v1.5
  473.          : 180  <--- v8.8
  474.  
  475.  *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  476.